xlv.obj.ajaxFrame={
	extend:function(){
	},
	implement:function(){
		this.implement('xlv.imp.ajax');
	},
	pub:{
		loading:'/xlive/images/_loading.gif'
	},
	create:function(tag){
		var div=document.createElement((tag||'div'));
		div.setAttribute('xlvobject','xlv.obj.ajaxFrame');
		return div;
	},
	properties:{
		description:'untitle',
		frameURL:null,
		frameProperties:null,
		frameMethod:null,
		_frameInstance:null,
		iconEffect:true,
		autoRequest:false
	},
	method:{
		initial:function(){
			Element.addClassName(this,'class_ajaxFrame');
			var p=this.$P;
			if(p.autoRequest)this.request(p.frameURL,p.frameProperties,p.frameMethod);
		},
		dispose:function(){
		},
		getTitle:function(){
			if(this.$P._frameInstance) return this.$P._frameInstance.getTitle();
			return this.$P.description;
		},
		request:function(url,properties,method){
			var p=this.$P;
			if(p._frameInstance){
				this.getDesktop().activeFrame(p._frameInstance);
				return true;
			}else{
				var v=this.$V;
				if(!url||url.strip().empty())return false;
				if(this.getDesktop().processExclusiveFrame(url))return true;
				//
				/*
				v.loading=document.createElement('img');
				v.loading.setAttribute('src',xlv.obj.ajaxFrame.pub.loading);
				var lt=xlv.desktop.viewportOffset(this);
				v.loading.style.position='absolute';
				v.loading.style.left=(lt[0])+'px';
				v.loading.style.top=(lt[1])+'px';
				v.loading.style.zIndex='1000';
				this.appendChild(v.loading);
				*/
				//
				v.loaded=false;
				v.url=url;
				v.properties=properties;
				v.method=method;
				this.ajax_request(url,{});
				return true;
			}
		},
		isLoading:function(){
			return this.$V.loading!=null;
		},
		isLoaded:function(){
			return this.$V.loaded===true;
		},
		loaded:function(){
			this.$V.loaded=true;
			var memo=xlv.sys.createMemoObject();
			memo.type='loaded';
			memo.src=this;
			memo.from=this.$V.url;
			memo.to=this.$V.frame;
			setTimeout((function(){Event.fire(this,'xlv:ajax_loaded',memo);}).bind(this),1);
		},
		getFrame:function(){
			return this.$V.frame;
		},
		_frameBounds:function(frame){
			var x=(frame.$P.left!=null)?frame.$P.left:parseInt(Element.getStyle(frame,'left'));
			var y=(frame.$P.top!=null)?frame.$P.top:parseInt(Element.getStyle(frame,'top'));
			var w=(frame.$P.width!=null)?frame.$P.width:parseInt(Element.getStyle(frame,'width'));
			var h=(frame.$P.height!=null)?frame.$P.height:parseInt(Element.getStyle(frame,'height'));
			if(!w||!h){
				var dim;
				if(Prototype.Browser.IE){
					Element.setStyle(frame.$V.header,{width:'100px'});
					Element.setStyle(frame.$V.root,{width:'auto'});
					dim=Element.getDimensions(frame);
					Element.setStyle(frame.$V.header,{width:'100%'});
					Element.setStyle(frame.$V.root,{width:'100%'});
				}else dim=Element.getDimensions(frame);
				w=(!w)?dim.width:w;
				h=(!h)?dim.height-frame._heightPadding():h;
			}
			if(!x||!y){
				var dim2=Element.getDimensions(this.getDesktop());
				x=(dim2.width-w)/2;
				x=(x<0)?0:x;
				y=(dim2.height-h)/2;
				y=(y<0)?0:y;
				var lt=this.getDesktop().viewportOffset();
				x -= lt.left;
				y -= lt.top;
			}
			return {left:x,top:y,width:w,height:h};
		},
		instantize:function(frame){
			this.$V.frame=frame;
			if(!this.$P.iconEffect){
				xlv.desktop.addFrame(frame);
				xlv.sys.initialize(frame);
				xlv.desktop.activeFrame(frame);
				this.loaded();
				return;
			}
			Element.setStyle(frame,{display:'none',position:'absolute'});
			frame.setUrl(this.$P.frameURL);
			xlv.desktop.addFrame(frame);
			var icon=document.createElement('img');
			xlv.desktop.appendChild(icon);
			icon.setAttribute('src',(frame.$P.iconURL)?frame.$P.iconURL:xlv.obj.frame.pub.icon);
			var lt=this.getDesktop().viewportOffset(this);
			Element.setStyle(icon,{position:'absolute',left:lt[0]+'px',top:lt[1]+'px',width:'32px',height:'32px'});
			var bounds=this._frameBounds(frame);
			var extend={element:frame};
			extend.style={width:bounds.width,height:bounds.height};
			extend.processed=function(){frame.layout();};
			extend.finished=(function(){this.loaded();}).bind(this);
			var move={element:icon};
			move.style={left:bounds.left,top:bounds.top};
			move.finished=function(){
				icon.parentNode.removeChild(icon);
				Element.setStyle(frame,{left:bounds.left+'px',top:bounds.top+'px',width:'24px',height:'24px',display:'block'});
				xlv.sys.initialize(frame);
				frame.getDesktop().activeFrame(frame);
				xlv.util.styleEffect(extend);
				};
			xlv.util.styleEffect(move);
		},
		ajax_response:function(aT,aJ){
			if(this.$V.loading){
				this.$V.loading.parentNode.removeChild(this.$V.loading);
				this.$V.loading=null;
			}
			var div=document.createElement('div');
			div.innerHTML=aT.responseText;
			var frame=xlv.util.findChild(div,{xlvobject:'xlv.obj.frame'},false,false);
			if(frame){
				xlv.sys.extend(frame,this.$V.properties,this.$V.method);
				this.instantize(frame);
				this.onSuccess();
			}else{
				frame=xlv.util.findChild(div,{xlvobject:/xlv.obj.borderLayout|xlv.obj.pane/},false,false);
				frame=(frame)?frame:div;
				frame.setAttribute('xlvobject','xlv.obj.frame');
				xlv.sys.extend(frame,this.$V.properties,this.$V.method);
				this.instantize(frame);
				this.onSuccess();
			}
		},		
		/**be overwrite**/
		onSuccess:function(){
		}
	}
};

